<?php

require_once 'modules/product/models/Product.php';
require_once 'modules/product/models/Input.php';
require_once 'modules/product/models/ProductProperty.php';

class product_IndexController extends Nine_Controller_Action
{
	public function indexAction()
	{
		$objProduct = new Models_Product();
		$objProductCategory = new Models_ProductCategory();
		
		/**
		 * Get category
		 */
		$categoryGid = $this->_getParam('cid',false);
		if (false == $categoryGid) {
			$this->_redirectToNotFoundPage();
		}
		
		$cat = $objProductCategory->getCategoryWithParent($categoryGid);
		if (null == $cat) {
			$this->_redirectToNotFoundPage();
		}
		
		$config = Nine_Registry::getConfig();
        $currentPage = $this->_getParam("page",false);
		
		/**
         * Get condition
         */
        $condition = $this->_getParam('condition', false);
        if (false === $condition) {
        	if (false != $currentPage) {
            	$condition = $this->session->productCondition;
        	}
        } else {
            $this->session->productCondition = $condition;
            $currentPage = 1;
        }
        if (false == $condition) {
            $condition = array();
        }
        
        if (false == $currentPage) {
        	$currentPage = 1;
        }
        
        
		/** 
		 * Set template
		 */
		$view = $this->_getParam('view',false);
		if (false === $view) {
            $view = $this->session->productView;
        } else {
            $this->session->productView = $view;
            $currentPage = 1;
        }
        if (false == $view) {
            $view = 'grid';
        }
		
		
		if ('grid' == $view) {
			$numRowPerPage = $config['gridNumberRowPerPage'];
			$templatePath  = Nine_Registry::getModuleName() . '/views/templates/grid';
			$templatePath .= '/index.' . Nine_Constant::VIEW_SUFFIX;
		}
		else if('list' == $view) {
			$numRowPerPage = $config['listNumberRowPerPage'];
			$templatePath  = Nine_Registry::getModuleName() . '/views/templates/list';
			$templatePath .= '/index.' . Nine_Constant::VIEW_SUFFIX;
		}
			
		/**
		 * Get all Products
		 */
		$allProducts = $objProduct->getAllEnabledProductsByCategory($categoryGid,$condition, array('sorting ASC','product_id DESC'),
                                                   $numRowPerPage,($currentPage - 1) * $numRowPerPage
                                                   );
        /**
         * Modified all products
         */
                                                   
        foreach($allProducts as &$product){
        	
        	$product['url'] = Nine_Route::_("product/index/detail/id/{$product['product_gid']}");
        	if ('grid' == $view) {
	        	if (null != $product['images']) {
	        		$tmp = explode("||", $product['images']);
	        		$product['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 258, 193,null,true);
	        		$product['full_image'] = Nine_Function::getThumbImage(@$tmp[0], 668,500,null,true);
	        	}
	        	else {
	        		$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noImage.jpg",258, 193,null,true);
	        		$product['full_image'] = Nine_Function::getThumbImage("media/userfiles/images/noImage.jpg",668,500,null,true);
	        	}
        	}
        	else if('list' == $view) {
        		if (null != $product['images']) {
	        		$tmp = explode("||", $product['images']);
	        		$product['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 98, 73,null,true);
	        		$product['full_image'] = Nine_Function::getThumbImage(@$tmp[0], 668,500,null,true);
	        	}
	        	else {
	        		$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noImage.jpg",98, 73,null,true);
	        		$product['full_image'] = Nine_Function::getThumbImage("media/userfiles/images/noImage.jpg",668,500,null,true);
	        	}
        	}
        	$product['price'] = @number_format($objProduct->makeUpPrice($product['price']),2);
        	
        }
        unset($product);
        
        /**
         * Count all products
         */
        $count = count($objProduct->getAllEnabledProductsByCategory($categoryGid,$condition));
        
        /**
         * Assign to rendered view
         */
        $this->view->headTitle(@$cat['name']);
        $this->view->menuId = @$cat['parent']['product_category_gid'];
        
        $this->setPagination($numRowPerPage, $currentPage, $count);
       	$this->view->cat = $cat;
        $this->view->allProducts = $allProducts;
        $this->view->condition = $condition;
        /**
         * All categories same level
         */
        $this->view->allCategories = $objProductCategory->getAllCategoryWithSameParent($cat['parent_id']);
        
//        echo "<pre>";print_r($this->view->allCategories);die;
        
		/**
		 * Render this template
		 */
		$this->view->html = $this->view->render( $templatePath );
	}
	
	
	public function detailAction() {
		
		$objProduct = new Models_Product();
		$objProductCategory = new Models_ProductCategory();
		$objProperty = new Models_ProductProperty();
		
		$gid = $this->_getParam('id',false);
		if (false == $gid) {
			$this->_redirectToNotFoundPage();
		}
		
		$product = $objProduct->getProductById( $gid );
		if (null == $product) {
			$this->_redirectToNotFoundPage();
		}
		/**
		 * Modified product
		 */
        
//        $cat = @reset($objProductCategory->getByColumns(array('product_category_gid=?'=>$product['product_category_gid']))->toArray());
		$cat = $objProductCategory->getCategoryWithParent($product['product_category_gid']);
        
        $product['category_name'] = $cat['name'];
		if ( null != @$product['images']) {
			$tmp = explode("||", $product['images']);
			$product['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 458,342,null,true);
//			$product['full_image'] = Nine_Function::getThumbImage(@$tmp[0], 800);
			$product['full_image'] = @$tmp[0];
		}
        else {
        	$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noImage.jpg", 458,342,null,true);
        	$product['full_image'] = Nine_Function::getThumbImage("media/userfiles/images/noImage.jpg", 800);
        }
		
		if (null != @$product['files']) {
        	$product['files'] = explode("||", trim($product['files'],"|| "));
        	foreach($product['files'] as &$file) {
        		$tmp = @end(explode("/", trim($file)));
        		$tmp2 = $file;
        		$file = array(
        				'name' => $tmp,
        				'url' => $tmp2
        				);
        	}
        	unset($file);
        }
        else {
        	$product['files'] = array();
        }
        
        
        if (null != @$product['price']) {
        	$product['price'] = @number_format($objProduct->makeUpPrice($product['price']),2);
        }
        
        
        $product['properties'] = $objProperty->getByColumns(array('product_gid=?'=>$gid))->toArray();
        
        
		$templatePath  = Nine_Registry::getModuleName() . '/views/templates/' . (Nine_Registry::getSession()->productView ? Nine_Registry::getSession()->productView : 'grid' );
		$templatePath .= '/detail.' . Nine_Constant::VIEW_SUFFIX;
		
        /**
         * Assign to rendered view
         */
        $this->view->headTitle($product['title']);
        $this->view->menuId = @$cat['parent']['product_category_gid'];
        $this->view->product = $product;
        
        /**
		 * Render this template
		 */
		$this->view->html = $this->view->render( $templatePath );
	}
	
	
	private function _redirectToNotFoundPage()
	{
	    $this->_redirect("");
	}
	
}